package com.dpj.Servlet;

import com.alibaba.fastjson.JSONObject;
import com.dpj.Service.BrandService;
import com.dpj.pojo.Brand;
import com.github.pagehelper.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/brand/*")
public class BrandServlet extends BaseServlet{
    private BrandService brandService=new BrandService();
        public void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // 设置响应数据的中文乱码
            response.setContentType("text/html;charset=utf-8");
            // 设置请求参数的中文乱码
            request.setCharacterEncoding("utf-8");
            BrandService brandService=new BrandService();
            List<Brand> brands=brandService.selectAll();
            String s = JSONObject.toJSONString(brands);
            response.getWriter().write(s);
        }


        public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            BufferedReader reader = req.getReader();
            String s = reader.readLine();
            System.out.println(s);
            int i=brandService.addOneBrand(s);
        }

        public void addOrUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            Brand brand = JSONObject.parseObject(req.getReader().readLine(), Brand.class);
           //1代表添加  2代表更新
            int i=brandService.addOrUpdate(brand);
            if(i==1){
                resp.getWriter().write("saveSuccess");
            }else {
                resp.getWriter().write("updateSuccess");
            }

        }

        public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            Brand brand = JSONObject.parseObject(req.getReader().readLine(), Brand.class);
            brandService.deleteById(brand.getId());
            resp.getWriter().write(brand.getBrandName());
        }

    public void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List list = JSONObject.parseObject(req.getReader().readLine(),List.class);
        brandService.deleteByIds(list);

    }

    public void  selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json; charset=utf-8");
        Integer pageNum = Integer.valueOf(req.getParameter("pageNum"));
        Integer pageSize = Integer.valueOf(req.getParameter("pageSize"));
        PageInfo<Brand> brandPageInfo = brandService.selectByPage(pageNum, pageSize);
        String s = JSONObject.toJSONString(brandPageInfo);

        resp.getWriter().write(s);

    }
    public void  selectByPageAndCondition(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json; charset=utf-8");
        Integer pageNum = Integer.valueOf(req.getParameter("pageNum"));
        Integer pageSize = Integer.valueOf(req.getParameter("pageSize"));
        System.out.println(pageNum);
        System.out.println(pageSize);
        String param = req.getReader().readLine();
        System.out.println(param);
        Brand brand = JSONObject.parseObject(param, Brand.class);
        System.out.println(brand);
        System.out.println("--------");
        PageInfo<Brand> brandPageInfo=  brandService.selectByPageAndCondition(pageNum,pageSize,brand);
        String s = JSONObject.toJSONString(brandPageInfo);
        resp.getWriter().write(s);
        System.out.println(s);

    }

}
